<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select">
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="orderList" stripe border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="200px"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status==='0'" type="danger">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180px">
          <template slot-scope="scope">
            {{scope.row.create_time|dataFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="editAddress(scope.row)"></el-button>
            <el-button size="mini" type="success" icon="el-icon-location" @click="logisticsProcess"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editAdreessVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editAddressForm" :rules="editAddressrules" ref="editAddressForm" label-width="100px">
        <el-form-item label="省市区/县:" prop="address1" placeholder="请选择">
          <el-cascader v-model="editAddressForm.address1" :options="Citydata" @change="handleChange" class="width"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址:" prop="address2" placeholder="请选择">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAdreessVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAdreessVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="logisticsProcessVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in process"
          :key="index"
          :timestamp="activity.timestamp">
          {{activity.address}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

  </div>
</template>

<script>
import Citydata from './citydata'
export default {
  name: 'Orders',
  created () {
    this.getOrderList()
  },
  data () {
    return {
      // 订单参数
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10
      },
      // 订单列表
      orderList: [],
      // 总条数
      total: 0,
      // 控制修改地址对话框的显示与隐藏
      editAdreessVisible: false,
      // 绑定到表单的对象
      editAddressForm: {
        // address1的值就是级联选择框选择的城市，是一个数组
        address1: '',
        // address2的值就是详细地址
        address2: ''
      },
      // 验证表单规则
      editAddressrules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      // 省市县/区
      Citydata,
      // 控制物流进度对话框的显示与隐藏
      logisticsProcessVisible: false,
      // 物流信息
      process: [{
        address: '活动按期开始',
        timestamp: '2018-04-15 06:11:11'
      }, {
        address: '通过审核',
        timestamp: '2018-04-13 00:11:11'
      }, {
        address: '创建成功',
        timestamp: '2018-04-11 00:00:00'
      }]
    }
  },
  methods: {
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error('获取订单列表失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 当前显示条数发生改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      // 更新页面
      this.getOrderList()
    },
    // 当前页发生改变时触发
    handleCurrentChange (current) {
      this.queryInfo.pagenum = current
      this.getOrderList()
    },
    // 点击编辑弹出修改地址对话框
    editAddress (row) {
      console.log(row)
      this.editAdreessVisible = true
    },
    // 级联选择器选择的省市县变化时触发
    handleChange () {
      console.log(this.editAddressForm.address1)
    },
    // 监听修改地址对话框关闭，清空文本
    editDialogClosed () {
      // 重置表单
      this.$refs.editAddressForm.resetFields()
    },
    // 显示物流进度对话框
    async logisticsProcess () {
      this.logisticsProcessVisible = true
      // 查看物流进度
      // const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      // console.log(res)
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 15px 0;
}
.width {
  width: 100%;
}
</style>
